// pages/coupon/coupon.js
var util = require('../../utils/request.js');
var globalData = getApp().globalData;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //停车场名字
    parkingName: '',
    //进入该页面前的页面
    way: '',
    //订单编号
    sn: '',
    discount: ['平台折扣', '停车场折扣'],
    DiscountCheck: 0,
    pageNumber: 1,
    pageSize: 10,
    discountList: [],
    hasMoreData: true,
    parkType: ''
  },
  //改变选中的折扣 tab
  chooseDiscount: function (e) {
    console.log(e);
    if (this.data.parkType != 'device') {
      this.setData({
        DiscountCheck: e.currentTarget.id,
        discountList: [],
        pageNumber: 1
      })
      console.log(e.currentTarget.id)
      if (e.currentTarget.id == 0) {
        this.getCoupon('platform');
      } else {
        this.getCoupon('parking');
      }
    } else {
      // this.getCoupon('platform');
    }
  },
  // 使用优惠券
  selectCoupon: function (e) {
    console.log(e)
    console.log(this.data.sn)
    var startTime = new Date(e.currentTarget.dataset.starttime).getTime();
    var nowTime = new Date().getTime();
    if (this.data.sn != '' && this.data.sn != undefined) {
      for (var i = 0; i < this.data.discountList.length; i++) {
        if (this.data.discountList[i].couponId == e.currentTarget.id) {
          //判定是否是该停车场的优惠卷
          if (nowTime >= startTime && ((this.data.DiscountCheck == 1 && this.data.parkingName == this.data.discountList[i].parking) || this.data.DiscountCheck == 0)) {
            var discountNum = this.data.discountList[i].discount
            var couponId = this.data.discountList[i].couponId
            wx.redirectTo({
              url: '../payOrder/payOrder?sn=' + this.data.sn + '&discountNum=' + discountNum + '&couponId=' + couponId
            })
          } else {
            wx.showToast({
              title: '不能使用该优惠卷!',
              icon: 'none'
            })
          }
        }
      }
    }

  },
  // 获取卡券
  getCoupon(dcouponType) {
    var that = this;
    var pageNumber = this.data.pageNumber;
    var pageSize = this.data.pageSize;
    var discountList = this.data.discountList;
    var data = {
      'memberId': globalData.memberId,
      'type': dcouponType,
      'pageNumber': pageNumber,
      'pageSize': pageSize
    }
    util.req('/member/coupon', data, function (res) {
      console.log(res);
      if (res.resultCode == 200) {
        res.resultContent.map(function (item) {
          item.expireDate = util.formatTime_year(new Date(item.expireDate));
          item.effectDate = util.formatTime_year(new Date(item.effectDate));
        })
        if (res.resultContent.length < pageSize) {
          that.setData({
            discountList: discountList.concat(res.resultContent),
            hasMoreData: false
          })
        } else {
          that.setData({
            discountList: discountList.concat(res.resultContent),
            hasMoreData: true,
            pageNumber: pageNumber + 1
          })
        }
      } else {
        wx.showToast({
          title: res.resultMsg,
        })
      }
      wx.hideLoading();
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    console.log(options);
    this.getCoupon('platform');
    if (options.sn != undefined) {
      this.setData({
        sn: options.sn,
        parkType: options.parkType,
        parkingName: options.parkingName
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    if (that.data.hasMoreData) {
      wx.showLoading({
        title: '正在加载...',
      })
      if (that.data.DiscountCheck == 0) {
        this.getCoupon('platform');
      } else {
        this.getCoupon('parking');
      }
    } else {
      wx.showToast({
        title: '没有更多数据!',
        icon: 'none',
        duration: 800
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})